package com.scalar.forecast.systemprefs.actioncommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.logger.Logger;
import com.scalar.forecast.commons.logger.LoggerManager;

import com.scalar.forecast.systemprefs.dao.SystemPrefsDAO;

import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.scalar.forecast.systemprefs.forms.SystemPreferencesForm;
import com.scalar.forecast.systemprefs.SystemPreferences;
import com.scalar.forecast.utils.CommonUtils;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;

/**
 * @author Basha
 * @version 1.0
 *
 */

public class SystemPreferencesModifyCommand extends ActionCommand
{
    private static final Logger LOGGER = LoggerManager.getLogger( SystemPreferencesModifyCommand.class );

   /**
     * Execute the SystemPreferencesModifyCommand
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws ScalarException
     */

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws ScalarException
    {
        ActionForward actionForward = mapping.findForward( "preferences_system" );

        SystemPreferencesForm  systemPreferencesForm = ( SystemPreferencesForm )form;
        SystemPreferences systemPreferences = systemPreferencesForm.getSystemPreferences();

        try
        {

            LOGGER.log(Logger.DEBUG, "In SystemModifyPreferencesCommand" );

            // setting the request attributes
            request.setAttribute( "buildMasterCantOverrideSFC" , CommonUtils.getCanBuildMasterOverrideSalesForecast() );
            request.setAttribute( "partNumberTypes" , CommonUtils.getPartNumberTypes() );
            request.setAttribute( "priceTypes" , CommonUtils.getPriceTypes() );

            systemPreferences = SystemPrefsDAO.loadSystemPreferences();
            systemPreferencesForm.setSystemPreferences( systemPreferences );
            systemPreferencesForm.setForecastTimeFrame( ForecastSetupDAO.getForecastTimeFrame( null ) );
        }

        catch( Exception ex )
        {
            LOGGER.log(Logger.DEBUG, "Error while processing the Request" );
        }

        return actionForward;
    }

}